<?php
namespace app\service;

class DecorationService {

    //单个瓷片的宽度
    protected $baseWidth;
    //单个瓷片的高度
    protected $baseHeight;
    /**要被计算的墙面宽高
     * @var
     * data格式
     * [
     *      [ 墙面1
     *          width => 墙面宽度
     *          height => 墙面高度
     *      ],
     *      [墙面2]
     * ]
     */
    protected $data;

    public function __construct($inputs)
    {
        $this->initParams($inputs);
    }

    public function initParams($inputs)
    {
        $this->baseWidth = array_get($inputs, 'baseWidth', 0);
        $this->baseHeight = array_get($inputs, 'baseHeight', 0);
        $this->data = array_get($inputs, 'data', []);
    }

    /**获取瓷片个数
     * @return float|int
     */
    public function getBlock()
    {
        $number = 0;
        foreach ($this->data as $block) {
            $number += $this->getNumbers($block['width'], $block['height']);
        }
        return $number;
    }

    /**根据宽高，获取瓷砖个数
     * @param $width
     * @param $height
     * @return float|int
     */
    public function getNumbers($width, $height)
    {
        //宽统一以0.77算,总宽是 0.77*0.7

        $baseWidth = $this->baseWidth;
        $baseHeight = $this->baseHeight;
        //计算宽, 不管长短，都补一个单个墙纸的宽度
        $lastWidth =
            ($width * 100) % ($baseWidth * 100) != 0 ?
                (floor($width / $baseWidth) + 1) * $baseWidth
                : (floor($width / $baseWidth) ) * $baseWidth;
        //计算宽, 不管高矮，都补一个单个墙纸的高度
        $lastHeight =
            ($height * 100) % ($baseHeight * 100) != 0 ?
                (floor($height / $baseHeight) + 1) * $baseHeight
                : (floor($height / $baseHeight) ) * $baseHeight;

        return ($lastHeight * $lastWidth) / ($baseHeight * $baseWidth);
    }

    /**获取面积
     * @return float|int
     */
    public function getArea()
    {
        $area = 0;
        foreach ($this->data as $block)
        {
            $area += $block['width'] * $block['height'];
        }

        return $area;
    }
}